function getFileName(path) {
  return path.slice(path.lastIndexOf('/') + 1);
}

function imgurlToImg(url) {
  return new Promise((resolve, reject) => {
    let img = document.createElement("IMG");
    img.onload = () => {
      resolve(img);
    };
    img.src = url;
  })
}

function imgToCanvas(img) {
  return new Promise((resolve, reject) => {
    let canvas = document.createElement('canvas');
    canvas.width = img.width;
    canvas.height = img.height;
    let ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    resolve(canvas);
  })
}

function canvasToBlob(canvas, quality, fn) {
  canvas.toBlob(function(blob){
    if(typeof fn === 'function') fn(blob)
  }, 'image/png', quality);
}

function sliceFile(file, start, end) {
  if(!(file instanceof File)) {
    console.error(`sliceFile, file-${file} is not an instance of File`);
  }
  let filename = file.name;
  let blob = file.slice(start, end);
  let newFile = new File([blob], filename);
  return newFile;
}

export default {
  imgurlToImg,
  imgToCanvas,
  canvasToBlob,
  getFileName,
  sliceFile
}